<?php
class Common {

    const
        P_LOG = '/log',
        P_PUBLIC = '/web';

    static public function getPathOf($path, $alia) {
        $path = ltrim($path, '/');
        return sprintf('%s/%s/%s', APP_ROOT, $alia, $path);
    }


    /**
     * 构建访问URL
     * @param string $path
     * @param null $include_host
     * @param bool $query
     * @return array|string
     */
    static public function httpUri($path = '/', $include_host = null, $query = false) {
        if (is_string($path)) {

            $uri = new \Common\URI($path);
            $http_entry = '';
            if (strlen(HTTP_ENTRY) > 0 && strpos($uri->path, HTTP_ENTRY) !== 0)
                $http_entry = HTTP_ENTRY;
            if (!$uri->scheme)
                $uri->path = $http_entry. '/' . ltrim($uri->path, '/\/');

            $relative_path = !$include_host;

            if ($include_host) {
                $uri->host = $include_host;
            }

            if ($query && is_array($query)) {
                if ($uri->query) {
                    $ori_query_params = $uri->get_query_params();
                    $query = array_merge($ori_query_params, $query);
                }
                $uri->query = http_build_query($query);
            }

            return $uri->build_url($relative_path);
        }
        else if (is_array($path))
            return array_map(array('Common', 'httpUri'), $path);
    }

    static public function log($content, $logFile = 'default.log') {
        $file = static::getPathOf($logFile, static::P_LOG);
        $dir = dirname($file);
        if (!is_dir($dir)) mkdir($dir, 0777, true);
        if ($content != null) {
            $dt = Date('Y-m-d H:i:s', time());
            if (is_array($content) || is_object($content))
                $content = var_export($content, 1);
            $content =  ' [' . $dt . ']' . ' ' . $content . "\r\n";
            file_put_contents($file, $content, FILE_APPEND);
        }
    }

    static public function init($http_entry) {
        if (!defined('HTTP_ENTRY')) {
            $http_entry = str_replace(DIRECTORY_SEPARATOR, '/', $http_entry);
            if ($http_entry == '/') {
                $http_entry = '';
            }
            define('HTTP_ENTRY', $http_entry);
        }
    }

    static public function view($path, $layout = 'layout.phtml') {
        if ($layout) {
            $content = $path;
            include_once(sprintf('%s/%s', VIEW_PATH, $layout));
            return;
        }
        include_once(sprintf('%s/%s', VIEW_PATH, $path));
    }

    static public function depth_get($data, $key, $default) {
        if (isset($data[$key])) {
            if (is_array($default)) {
                return array_merge($default, $data[$key]);
            } else {
                return $data[$key];
            }
        }

        return $default;
    }

    static public function get($keys = null, $default = null) {
        return static::depth_get($_GET, $keys, $default);
    }

    static public function post($keys = null, $default = null) {
        return static::depth_get($_POST, $keys, $default);
    }

    static public function redirect($url) {
        header('Location:'.$url);
        exit;
    }

    static public function subset() {
        $font_name = self::get('name');
        $subset = self::get('subset');
        $callback_id = self::get('callback_id');
        $css_path = '';
        if ($font_name && $subset) {
            $css_path = FontHelper::subset_font($font_name, $subset);
        }

        return array($callback_id, $css_path);
    }
} 